#include <iostream>
#include <string>
#include <unordered_map>
#include <ctime>
#include <cstdlib>
#include <openssl/sha.h>
#include <sstream>
#include <fstream>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>

// 哈希函数，用于生成验证码
std::string hashUsername(const std::string& username) {
    unsigned char hash[SHA256_DIGEST_LENGTH];
    SHA256_CTX sha256;
    SHA256_Init(&sha256);
    SHA256_Update(&sha256, username.c_str(), username.size());
    SHA256_Final(hash, &sha256);
    std::ostringstream oss;
    for(int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
        oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
    }
    return oss.str();
}

// 发送邮件函数，简单模拟邮件发送，实际需要邮件服务器支持
void sendVerificationEmail(const std::string& to, const std::string& verifyLink) {
    // 这里仅为示例，实际实现需要连接邮件服务器，使用SMTP协议发送邮件
    std::cout << "Sending verification email to: " << to << " with link: " << verifyLink << std::endl;
}

// 检查昵称是否可用，简单示例，实际应连接数据库检查
bool isNicknameAvailable(const std::string& nickname) {
    // 假设从文件中读取已注册昵称，实际应从数据库查询
    std::ifstream file("registered_nicknames.txt");
    std::string line;
    while (std::getline(file, line)) {
        if (line == nickname) {
            file.close();
            return false;
        }
    }
    file.close();
    return true;
}

// 注册用户函数
bool registerUser(const std::string& nickname, const std::string& password, const std::string& email) {
    if (!isNicknameAvailable(nickname)) {
        std::cout << "Nickname is already taken." << std::endl;
        return false;
    }
    std::string code = hashUsername(nickname);
    std::string verifyLink = "http://127.0.0.1/verify?user=" + nickname + "&code=" + code;
    sendVerificationEmail(email, verifyLink);

    // 将用户信息保存到文件，实际应保存到数据库
    std::ofstream file("users.txt", std::ios::app);
    file << nickname << " " << password << " " << email << " " << code << " " << time(nullptr) << std::endl;
    file.close();

    std::cout << "Registration successful. Verification email sent." << std::endl;
    return true;
}

// 验证用户函数
bool verifyUser(const std::string& nickname, const std::string& code) {
    std::ifstream file("users.txt");
    std::string line;
    while (std::getline(file, line)) {
        std::istringstream iss(line);
        std::string storedNickname, storedPassword, storedEmail, storedCode;
        time_t storedTime;
        iss >> storedNickname >> storedPassword >> storedEmail >> storedCode >> storedTime;
        if (storedNickname == nickname && storedCode == code && time(nullptr) - storedTime < 3600) {
            file.close();
            // 验证成功，可进行激活操作，如更新数据库状态
            std::cout << "Verification successful. User activated." << std::endl;
            return true;
        }
    }
    file.close();
    std::cout << "Verification failed. Invalid link or link has expired." << std::endl;
    return false;
}

int main() {
    // 简单模拟注册和验证流程
    std::string nickname = "hw";
    std::string password = "password123";
    std::string email = "test@example.com";

    if (registerUser(nickname, password, email)) {
        // 模拟点击验证链接
        std::string verifyNickname = "hw";
        std::string verifyCode = hashUsername(verifyNickname);
        verifyUser(verifyNickname, verifyCode);
    }

    return 0;
}